package com.xiaowu.datajpa.service;
import com.xiaowu.datajpa.dao.CourseRepository;
import com.xiaowu.datajpa.dao.StudentCourseRepository;
import com.xiaowu.datajpa.entity.Course;
import com.xiaowu.datajpa.entity.DateShow;
import com.xiaowu.datajpa.entity.Student;
import com.xiaowu.datajpa.entity.StudentCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentCourseSercvice {
    @Autowired
    private StudentCourseRepository studentCourseRepository;
    @Autowired
    private CourseSercvice courseSercvice;
    @Autowired
    private StudentSercvice studentSercvice;


    /**
     * 查询所有学生选课信息
     * @return
     */
    public List<DateShow> findAll(){
        List<StudentCourse> list = studentCourseRepository.findAll();
        List<DateShow> scList = new ArrayList<>();
        for (StudentCourse sc : list) {
            String studentnum = sc.getStudentnum();
            Student s = studentSercvice.findByStunumber(studentnum);
            String coursenum = sc.getCoursenum();
            Course c = courseSercvice.findById(coursenum);
            DateShow ds = new DateShow();
            ds.setId(sc.getId());
            ds.setStudentnum(s.getStunumber());
            ds.setStudentname(s.getName());
            ds.setCoursenum(c.getCoursenum());
            ds.setCoursename(c.getName());
            ds.setTeachername(c.getTeacher());
            scList.add(ds);
        }
        return scList;
    }


}
